// organizer.js

var Organizer = (function () {

    var singletonClass = {
        init: function () {
            this.log = [];
            //this.renderer;

            var lists = Organizer.Storage.TaffyDB.getAllLists();
            
            var html, list;
            for (var i = 0, len = lists.length; i < len; i++) {

                list = lists[i];
                html = '<button>' + list.name + '</button>';
                $('#organizer .main-sidebar .middle ul:first-child').append(html);
            }
            /////////
            // add lists from the database to the html



            // list entries according to the selected period + list:
            // ('all lists: all' initially)
            return this;
        },
        addTask: function(){
            var newTask = Object.create(Organizer.Entries.Task).init("Test","test description","high","12/21/2012");
            //$.append(newTask.queryObj);
        },
        addEvent: function(){

        },
        addAnniversary: function(){

        },
        view: function(){
            //this.renderer.izpliushti();
        },
        initEvents: function(){
            $('#organizer .main-sidebar .middle button').on('click',function(){
                $("#organizer .main-sidebar .middle .active").removeClass("active");
                $(this).addClass('active');
                var entries = Organizer.Storage.TaffyDB.getList(this.innerHTML);

                $('#list-container').html(" ");
                this.currentList = Object.create(Organizer.EntryList).init(this.innerHTML);

                                                
                for(var i=0;i<entries.length;i+=1){
                    if(entries[i].type=="event"){

                        var startDate = entries[i].startdate_day+"/"+entries[i].startdate_month + "/" + entries[i].startdate_year;
                        var endDate = entries[i].enddate_day+"/"+entries[i].enddate_month + "/" + entries[i].enddate_year;
                        var currentEntry = Object.create(Organizer.Entries.Event).init(entries[i].title,entries[i].description,entries[i].priority,startDate,endDate,entries[i].location);
                    
                    }else if(entries[i].type="task"){

                        var currentEntry = Object.create(Organizer.Entries.Task).init(entries[i].title,entries[i].description,entries[i].priority,entries[i].enddate_day+"/"+entries[i].enddate_month+"/"+entries[i].enddate_year);
                    
                    }else if(entries[i].type="anniversary"){

                        var currentEntry = Object.create(Organizer.Entries.Ann).init();
                    }
                    this.currentList.addEntry(currentEntry);
                }

                this.currentList.visualize();
            });

        }
    }

    var instance;
    return {
        getInstance: function () {
            if (!instance) {
                instance = Object.create(singletonClass);

                // Hide the constructor so the returned objected can't be new'd...
                instance.constructor = null;
            }
            return instance;
        }
    };
})();
